package com.ctrip.demo.tree;

import org.joda.time.DateTime;

import javax.swing.plaf.PanelUI;
import java.sql.Timestamp;
import java.util.*;
import java.util.stream.Collectors;

public class TreeTest {

    public static void main(String[] args) {
//        List<Node> tree = new TreeTest().getTree();
//        for (Node node : tree) {
//            System.out.println(node);
//        }
//        System.out.println(tree);
//
//        Set<String> set = new HashSet<>();
//
//        set.add("张三");
//        set.add("李四");
//        System.out.println(set.toString());

        DateTime dateTime = new DateTime().minusHours(1).toDateTime();
        long timeInMillis = dateTime.toCalendar(Locale.CHINA).getTimeInMillis();

        System.out.println(new Timestamp(timeInMillis));

        DateTime dateTime1 = DateTime.now().minusHours(5);
        System.out.println(new Timestamp(dateTime1.getMillis()));


        List<Item> strings = new ArrayList<>();
        strings.add(new Item(null, "xxx"));
        strings.add(new Item(null, "a"));
        strings.add(null);
        strings.add(new Item("xxx","xxx"));

        strings.forEach(System.out::println);

        System.out.println("filter");
        strings = strings.stream().filter(item -> (null != item && item.text != null)).collect(Collectors.toList());
        strings.forEach(System.out::println);

    }

    public static class Item {
        public String text;
        public String imId;

        public Item(String text,String imId) {
            this.imId = imId;
            this.text = text;
        }

        @Override
        public String toString() {
            return "Item{" +
                    "text='" + text + '\'' +
                    ", imId='" + imId + '\'' +
                    '}';
        }
    }

    public List<Node> getTree() {
        List<Node> nodeList = getNodeList();
        List<Node> tree = buildTree(nodeList);
        return tree;
    }

    // 初始化数据集合
    private List<Node> getNodeList() {
        List<Node> list = new ArrayList<Node>();
        Node node1 = new Node(1, 0, "公司库", "股票");
        Node node2 = new Node(2, 0, "基金库", "基金");
        Node node3 = new Node(111, 1, "A股市场", "股票");
        Node node4 = new Node(112, 1, "港股市场", "股票");
        Node node11 = new Node(1111, 111, "A股市场111", "股票");
        Node node5 = new Node(211, 2, "公墓基金池", "基金");
        Node node6 = new Node(212, 2, "非公墓基金池", "基金");
        Node node7 = new Node(11111, 111, "基础池", "股票");
        Node node8 = new Node(21211, 212, "可买池", "基金");
        list.add(node1);
        list.add(node2);
        list.add(node3);
        list.add(node4);
        list.add(node5);
        list.add(node11);
        list.add(node6);
        list.add(node7);
        list.add(node8);
        return list;
    }

    // 封装集合为树形结构
    private List<Node> buildTree(List<Node> nodes) {
        Map<Integer, List<Node>> children = nodes.stream().filter(node -> node.getPid() != 0)
                .collect(Collectors.groupingBy(node -> node.getPid()));
        nodes.forEach(node -> node.setChildren(children.get(node.getId())));
        List<Node> collect = nodes.stream().filter(node -> node.getPid() == 0).collect(Collectors.toList());
        return collect;
    }

}
